package com.housekeeping.service.impl;

import com.housekeeping.dao.CategoryDAO;
import com.housekeeping.dao.HousekeepingServiceDAO;
import com.housekeeping.dao.impl.CategoryDAOImpl;
import com.housekeeping.dao.impl.HousekeepingServiceDAOImpl;
import com.housekeeping.model.Category;
import com.housekeeping.model.HousekeepingService;
import com.housekeeping.service.HousekeepingServiceService;
import java.util.Date;
import java.util.List;

/**
 * 家政服务服务实现类
 */
public class HousekeepingServiceServiceImpl implements HousekeepingServiceService {
    
    private HousekeepingServiceDAO serviceDAO = new HousekeepingServiceDAOImpl();
    private CategoryDAO categoryDAO = new CategoryDAOImpl();

    @Override
    public HousekeepingService getServiceById(Integer serviceId) {
        return serviceDAO.findById(serviceId);
    }

    @Override
    public List<HousekeepingService> getAllServices() {
        return serviceDAO.findAll();
    }

    @Override
    public List<HousekeepingService> getServicesByCategory(Integer categoryId) {
        return serviceDAO.findByCategoryId(categoryId);
    }

    @Override
    public List<HousekeepingService> getAvailableServices() {
        return serviceDAO.findAvailableServices();
    }

    @Override
    public List<HousekeepingService> searchServices(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return getAvailableServices();
        }
        return serviceDAO.searchByKeyword(keyword.trim());
    }

    @Override
    public boolean addService(HousekeepingService service) {
        if (service == null || service.getServiceName() == null || service.getPrice() == null) {
            return false;
        }
        
        
        if (service.getStatus() == null) {
            service.setStatus(1); 
        }
        if (service.getUnit() == null) {
            service.setUnit("次");
        }
        service.setCreateTime(new Date());
        
        return serviceDAO.insert(service) > 0;
    }

    @Override
    public boolean updateService(HousekeepingService service) {
        if (service == null || service.getServiceId() == null) {
            return false;
        }
        service.setUpdateTime(new Date());
        return serviceDAO.update(service) > 0;
    }

    @Override
    public boolean deleteService(Integer serviceId) {
        return serviceDAO.delete(serviceId) > 0;
    }

    @Override
    public boolean updateServiceStatus(Integer serviceId, Integer status) {
        return serviceDAO.updateStatus(serviceId, status) > 0;
    }

    @Override
    public boolean publishService(Integer serviceId) {
        return updateServiceStatus(serviceId, 1); 
    }

    @Override
    public boolean unpublishService(Integer serviceId) {
        return updateServiceStatus(serviceId, 0); 
    }

    @Override
    public long getServiceCount() {
        return serviceDAO.count();
    }

    @Override
    public List<Category> getAllCategories() {
        return categoryDAO.findAll();
    }

    @Override
    public List<Category> getActiveCategories() {
        return categoryDAO.findActiveCategories();
    }

    @Override
    public boolean addCategory(Category category) {
        if (category == null || category.getCategoryName() == null) {
            return false;
        }
        
        
        if (category.getStatus() == null) {
            category.setStatus(1); 
        }
        if (category.getSort() == null) {
            category.setSort(0);
        }
        category.setCreateTime(new Date());
        
        return categoryDAO.insert(category) > 0;
    }

    @Override
    public boolean updateCategory(Category category) {
        if (category == null || category.getCategoryId() == null) {
            return false;
        }
        return categoryDAO.update(category) > 0;
    }

    @Override
    public boolean deleteCategory(Integer categoryId) {
        return categoryDAO.delete(categoryId) > 0;
    }
} 